package com.kyzh.core.pager.weal.growth

import android.content.res.ColorStateList
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.beans.GrowthBean
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.FragGrowthBinding
import com.kyzh.core.databinding.ItemGrowthBinding
import com.gushenge.core.base.fragment.BaseFragment

class GrowthFragment : BaseFragment() {
    private val  adapter = Adapter()
    private lateinit var db :FragGrowthBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db  = FragGrowthBinding.inflate(inflater)
        return db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.rev.adapter =adapter
//        initData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if(!hidden){
            initData()
        }
    }

    override fun onResume() {
        super.onResume()
        initData()
    }
    fun initData(){
        WealRequest.Growth {
            adapter.setNewInstance(this)
        }
    }
    private class Adapter : BaseQuickAdapter<GrowthBean, BaseDataBindingHolder<ItemGrowthBinding>>(R.layout.item_growth){
        override fun convert(holder: BaseDataBindingHolder<ItemGrowthBinding>, item: GrowthBean) {
            holder.dataBinding?.apply {
                data = item
                when (item.status){
                    "0"->{
                        tvStatus.text = "未完成"
                        tvStatus.setTextColor(Color.WHITE)
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp)
                        tvStatus.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#CCCCCC"))
                    }
                    "1" ->{
                        tvStatus.text = "已完成"
                        tvStatus.setTextColor(Color.parseColor("#CCCCCC"))
                        tvStatus.setBackgroundResource(R.drawable.bg_14dp)
                        tvStatus.backgroundTintList = ColorStateList.valueOf(Color.parseColor("#F7F7F7"))
                    }
                }
            }
        }
    }
}